const app = getApp();
import api from "../../utils/api_util";
import tip from "../../utils/tip";
import baseUtil from "../../utils/base_util";

Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        codeText: '获取验证码',
        bingphone: '',
        msgCode: '',
        inviteCode: '',
        isLogin: false,

        //微信授权
        wxLogincode: "",
        loginBtn: 1, //2_第一次登录需要调微信授权。3_注册
    },
    onLoad: function (options) {
    },
    // input监听手机号
    bingPhone(e) {
        this.setData({
            bingphone: e.detail.value
        })
    },
    jumpPage: function () {

    },
    //code
    bindCode: function (e) {
        this.setData({
            bingphone: e.detail.value
        })
    },
    //短信验证码
    msgCode: function (e) {
        this.setData({
            msgCode: e.detail.value
        })
    },
    //邀请码
    inviteCodeChange: function (e) {
        this.setData({
            inviteCode: e.detail.value
        })
    },
    //登录
    loginPost: function () {
        let that = this;

        if(that.data.bingphone < 0 || that.data.bingphone == ''){
            tip.alert("请输入手机号~");
            return;
        }
        if(that.data.msgCode < 0 || that.data.msgCode == ''){
            tip.alert("请填写验证码~");
            return;
        }
        api.wxRequest.apiRequest(that, api.urls.userLogin, {
            method: 'POST',
            data: {
                v: '1.0.0',
                mobile: that.data.bingphone,
                code: that.data.msgCode,
                inviteCode: that.data.inviteCode,
            },
            success: function (res) {
                baseUtil.pageJump('/pages/Index/Index', true);
            },
            code: function (res) {
            },
            headerInfo: function (res) {
                baseUtil.setStorageSync('token', res.token);
            }
        })
    },
    // 获取手机发送验证码
    getPhoneCodePost: function (e) {
        let that = this;
        let bingphone = that.data.bingphone;
        let type = e.currentTarget.dataset.type;

        let isPass = /^[1][3,4,5,7,8][0-9]{9}$/.test(bingphone);
        if (!isPass) {
            wx.showToast({
                title: '手机号有误，请检查后确认',
                icon: 'none'
            });
            return
        }


        api.wxRequest.apiRequest(that, api.urls.checkWxApplet, {
            method: 'GET',
            data: {
                v: '1.0.0',
                mobile: bingphone,
            },
            success: function (res) {

                if (res.data.result) {
                    that.data.loginBtn = 1;
                } else {
                    that.data.loginBtn = 2;
                }

                api.wxRequest.apiRequest(that, api.urls.sendVerifyCode, {
                    method: 'POST',
                    data: {
                        v: '1.0.0',
                        mobile: bingphone,
                        type: 1,
                    },
                    success: function (res) {
                        that.getPhoneCode();
                    },
                });

                that.setData({
                    loginBtn: that.data.loginBtn,
                })
            },
        })

    },
    getPhoneCode: function () {
        let that = this;

        let time = 60;
        setTimeout(function () {

            tip.alert("短信验证码已发出，请稍等");

        }, 100);
        that.setData({
            codeText: '60秒后重发',
            disabled: true
        })
        var Interval = setInterval(function () {
            time--;
            if (time > 0) {
                that.setData({
                    codeText: time + '秒后可重发'
                })
            } else {
                clearInterval(Interval);
                that.setData({
                    codeText: '获取验证码',
                    disabled: false
                })
            }
        }, 1000)
    },


    /**
     * 微信授权
     */
    onGotUserInfo: function (e) {
        let that = this;

        if(that.data.bingphone < 0 || that.data.bingphone == ''){
            tip.alert("请输入手机号~");
            return;
        }
        if(that.data.msgCode < 0 || that.data.msgCode == ''){
            tip.alert("请填写验证码~");
            return;
        }
        wx.login({
            success: function (res) {

                console.log('res.code:', res.code);

                let wxLogincode = res.code;

                api.wxRequest.apiRequest(that, api.urls.userLogin, {
                    method: 'POST',
                    data: {
                        v: '1.0.0',
                        mobile: that.data.bingphone,
                        code: that.data.msgCode,
                        inviteCode: that.data.inviteCode,
                    },
                    success: function (res) {},
                    code: function (res) {},
                    headerInfo: function (res) {
                        baseUtil.setStorageSync('token', res.token);
                        wx.request({
                            url: api.urls.bindWxApplet,
                            header: {
                                "Content-Type": "application/x-www-form-urlencoded",
                                'token': baseUtil.getStorageSync('token') ? baseUtil.getStorageSync('token') : '',
                            },
                            method: 'POST',
                            data: {
                                v: '1.0.0',
                                jsCode: wxLogincode,
                            },
                            success: function (response) {
                                // if (response.data.result) {
                                //     that.wxGetUserInfo();
                                //     console.log("进");
                                // }
                                if (response.data.code == "199999") {
                                    that.wxGetUserInfo();
                                    console.log("进");
                                }
                            },
                            fail: function (error) {},
                            complete: function (response) {}
                        });

                    }
                })

            },
        });
    },
    wxGetUserInfo: function (e) {
        let that = this;
        let userInfo;
        baseUtil.loading();
        wx.getUserInfo({
            success: function (data) {
                // tip.loaded();
                console.log('data:', data);

                userInfo = data.userInfo;

                console.log('userInfo:', userInfo);
                api.wxRequest.apiRequest(that, api.urls.updateInfo, {
                    method: 'POST',
                    data: {
                        nickname: userInfo.nickName,
                        headimg: userInfo.avatarUrl,
                        v: "1.0.0"
                    },
                    success: function (response) {
                        baseUtil.pageJump('/pages/Index/Index', true);
                    },

                });
            },
            fail: function () {
                console.log('用户点了拒绝');
            },
            complete: function () {
                baseUtil.loaded();
            }
        })
    },

    //注册
    showPhoneModal: function () {

        let that = this;
        wx.redirectTo({
            url: '/pages/register/register'
        })


    },
})